/*DIctConverter方法*/
import DictMeta from '@/utils/dict/DictMeta.ts'
import Dictoptions from './DictOptions.ts'
import DictData from './DictData.ts'

export default function (dict: any, dictMeta: DictMeta) {
  /*对dict进行处理*/
  const label = determineDictField(
    dict,
    dictMeta.labelField,
    ...Dictoptions.DEFAULT_LABEL_FIELDS,
  )
  const value = determineDictField(
    dict,
    dictMeta.valueField,
    ...Dictoptions.DEFAULT_VALUE_FIELDS,
  )
  return new DictData(dict[label], dict[value], dict)
}

/**
 * 确定字典字段
 * @param {DictData} dict
 * @param  {...String} fields
 */
function determineDictField(dict: any, ...fields: any) {
  return fields.find((f: any) => Object.prototype.hasOwnProperty.call(dict, f))
}
